var express = require('express');
var router = express.Router();


// 引入封装的 mysql模块
const mysql = require('../util/mysql/mysql');
const sql = require('../util/mysql/sql');

//分类
router.post('/selClassify', function (req, res, next) {
  mysql.blog(sql.Classify, [req.body.product_variety], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//搜索商品
router.post('/search', function (req, res, next) {
  mysql.blog(sql.search, ['%' + req.body.product_name + '%'], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify(rows));
  });
});
//同城
router.post('/selLocal', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 12;
  let end = page * 12;
  mysql.blog(sql.Local, [], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
  });
});
//同城分类
router.post('/selLocal/classify', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 12;
  let end = page * 12;
  if (req.body.local_classify === '全部') {
    mysql.blog(sql.Local, [], function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
    });
  } else {
    mysql.blog(sql.LocalClassify, [req.body.local_classify], function (err, rows, fields) {
      if (err) return;
      res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
    });
  }
});
//礼拜五
router.post('/selFriday', function (req, res, next) {
  let page = req.body.page;
  let start = (page - 1) * 8;
  let end = page * 8;
  mysql.blog(sql.Classify, [req.body.product_variety], function (err, rows, fields) {
    if (err) {
      return err
    };
    res.send(JSON.stringify({ rows_length: rows.length, rows: rows.slice(start, end) }));
  });
});
//积分商城
router.post('/selIntegralProduct', function (req, res, next) {
  mysql.blog(sql.selIntegralProduct, [], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//积分商城商品
router.post('/selIntegral/single', function (req, res, next) {
  mysql.blog(sql.selIntegralSingle, [req.body.integral_id], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify(rows));
  });
});
//积分商品评价
router.post('/selIntegral/evaluate', function (req, res, next) {
  mysql.blog(sql.selIntegralEvaluate, [req.body.integral_id], function (err, rows, fields) {
    if (err) {
      console.log(err);
    };
    res.send(JSON.stringify(rows));
  });
});
//查询商店
router.post('/selStore', function (req, res, next) {
  mysql.blog(sql.store, [], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//购物车
router.post('/cart', function (req, res, next) {
  mysql.blog(sql.Cart, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//添加确认订单
router.post('/ensure', function (req, res, next) {
  mysql.blog(sql.Ensure, [
    req.body.store_name,
    req.body.user_id,
    req.body.totalPrice,
    req.body.product
  ], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '已添加到确认订单' }));
  });
});
//查看确认订单
router.post('/selEnsure', function (req, res, next) {
  mysql.blog(sql.selEnsure, [req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//删除确认订单
router.post('/delEnsure', function (req, res, next) {
  mysql.blog(sql.delEnsure, [req.body.ensure_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '删除了' }));
  });
});
//添加购物车
var id = ''
router.post('/addCart', function (req, res, next) {
  let product = JSON.parse(req.body.product)
  if (product.product_id == id) {
  } else {
    mysql.blog(sql.addCart, [
      req.body.store_name,
      req.body.user_id,
      req.body.cart_totalPrice,
      req.body.product
    ], function (err, rows, fields) {
      if (err) {
        console.log(err);
      };
      res.send(JSON.stringify({ msg: '已添加到购物车' }));
      id = product.product_id;
    });
  }

});
// 删除购物车商品
router.post('/delgoods', function (req, res, next) {
  mysql.blog(sql.delGoods, [req.body.cart_id], function (err, rows, fields) {
    if (err) console.log(err);
    res.send(JSON.stringify({ code: 1, msg: '已成功删除' }));
  });
});
//验证用户
router.post('/verify', function (req, res, next) {
  mysql.blog(sql.verify, [req.body.user_id, req.body.user_pass], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//付钱
router.post('/pay', function (req, res, next) {
  mysql.blog(sql.pay, [req.body.payMoney, req.body.payMoney, req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '你出血了' }));
  });
});
// 积分付钱
router.post('/payIntegral', function (req, res, next) {
  mysql.blog(sql.payIntegral, [req.body.payMoney, req.body.user_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify({ msg: '你出血了' }));
  });
});
//商品信息
router.post('/product', function (req, res, next) {
  mysql.blog(sql.selProduct, [req.body.product_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});
//商品评价
router.post('/product/evaluate', function (req, res, next) {
  mysql.blog(sql.evaluate, [req.body.product_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});

// 通过地址ID查询地址
router.post('/getaddress', function (req, res, next) {
  mysql.blog(sql.getAddress, [req.body.address_id], function (err, rows, fields) {
    if (err) return;
    res.send(JSON.stringify(rows));
  });
});

module.exports = router;
